aboutsummaryrefslogtreecommitdiff
path: root/src/routes/hololive/[[stream]]/+page.svelte
blob: 777f1e709f00d65901f32e6d7b39753ea5960edd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<script lang="ts">
	import { onMount } from 'svelte';
	import Message from '$lib/Loading/Message.svelte';
	import Skeleton from '$lib/Loading/Skeleton.svelte';
	import HeadTitle from '$lib/Home/HeadTitle.svelte';
	import { parseScheduleHtml } from '$lib/Data/hololive';
	import proxy from '$lib/Utility/proxy';
	import locale from '$stores/locale';
	import root from '$lib/Utility/root';
	import identity from '$stores/identity';
	import Lives from '$lib/Hololive/Lives.svelte';
	import { typeSchedule } from '$lib/Hololive/hololive';

	export let data;

	let schedulePromise: Promise<Response>;
	let pinnedStreams: string[] = [];

	onMount(() => getPinnedStreams());

	const getPinnedStreams = () => {
		let streams: string[] = [];

		const setSchedule = () => {
			pinnedStreams = streams;
			schedulePromise = fetch(proxy('https://schedule.hololive.tv'), {
				headers: {
					Cookie: 'timezone=Asia/Tokyo'
				}
			});
		};

		if ($identity.id !== -2) {
			fetch(root(`/api/preferences?id=${$identity.id}`)).then((response) => {
				if (response.ok)
					response.json().then((data) => {
						if (data && data.pinned_hololive_streams) streams = data.pinned_hololive_streams;

						setSchedule();
					});
			});

			return;
		}

		setSchedule();
	};
</script>

<HeadTitle route="hololive Schedule" path="/hololive" />

{#await schedulePromise}
	<Message message="Loading schedule ..." />

	<Skeleton grid={true} count={100} width="49%" height="16.25em" />
{:then scheduleResponse}
	{#if scheduleResponse}
		{#await scheduleResponse.text()}
			<Message message="Parsing schedule ..." />

			<Skeleton grid={true} count={100} width="49%" height="16.25em" />
		{:then untypedSchedule}
			{@const schedule = typeSchedule(parseScheduleHtml(untypedSchedule))}

			<Lives {schedule} {pinnedStreams} {getPinnedStreams} filter={data.stream} />
		{:catch}
			<Message loader="ripple" slot>
				{$locale().hololive.parseError}
				<a href={'#'} on:click={() => location.reload()}>Try again?</a>
			</Message>
		{/await}
	{:else}
		<Message message="Loading schedule ..." />

		<Skeleton grid={true} count={100} width="49%" height="16.25em" />
	{/if}
{:catch}
	<Message loader="ripple" slot>
		{$locale().hololive.loadError} Please
		<a href={'#'} on:click={() => location.reload()}>try again</a> later.
	</Message>
{/await}